﻿package com.thorqq.magictimer;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.thorqq.magictimer.SettingActivityTimer.LoopPolicyButtonListener;
import com.thorqq.magictimer.SettingActivityTimer.LoopPolicyDeleteListener;
import com.thorqq.magictimer.SettingActivityTimer.LoopPolicyLongClickListener;
import com.thorqq.magictimer.animation.ExpandAnimation;
import com.thorqq.magictimer.core.ChildView;
import com.thorqq.magictimer.core.ChildViewParent;
import com.thorqq.magictimer.core.TLoopPolicy;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import android.widget.TextView;

public class AdapterLoopPolicy extends BaseAdapter
{

    public final class ViewHolder
    {
        public TLoopPolicy policy;
        public Button excludeFlag;
        public TextView policyInfo;
        public TextView policyInfoMore;
        public Button deletePolicy;
        public LinearLayout layoutPolicyDetail;
        public ImageView show;

        public ChildView childView = null;
        public ChildViewParent parent;
        public int visibility = View.GONE;
        public LinearLayout layoutDown;
        public ExpandAnimation expandAni;
    }

    private LayoutInflater mInflater;
    private List<Map<String, Object>> mData;
    //private TLoopPolicy mPolicy;

    public AdapterLoopPolicy(Context context)
    {
        mInflater = LayoutInflater.from(context);
        mData = new ArrayList<Map<String, Object>>();
    }

    public AdapterLoopPolicy(Context context, List<Map<String, Object>> data)
    {
        mInflater = LayoutInflater.from(context);
        mData = data;
    }

    public void add(Map<String, Object> map)
    {
        mData.add(map);
    }

    public void removeItem(int position)
    {
        mData.remove(position);
    }
    
    public void updateItem(int position, String key, Object obj)
    {
        if (position < mData.size())
        {
            mData.get(position).put(key, obj);
        }
    }

    public int getCount()
    {
        return mData.size();
    }

    public Object getItem(int position)
    {
        if (position < mData.size())
        {
            return mData.get(position);
        }
        else
        {
            return null;
        }
    }

    public long getItemId(int position)
    {
        if (position < mData.size())
        {
            return position;
        }
        else
        {
            return -1;
        }
    }
    
    public View getView(final int position, View convertView, ViewGroup parent)
    {
        ViewHolder holder = null;
        if (convertView == null)
        {
            holder = new ViewHolder();

            convertView = mInflater.inflate(R.layout.loop_item, null);
            holder.excludeFlag  = (Button) convertView.findViewById(R.id.btnExcludeFlag);
            holder.policyInfo = (TextView) convertView.findViewById(R.id.tvPolicyInfo);
            holder.policyInfoMore = (TextView) convertView.findViewById(R.id.tvPolicyInfoMore);
            holder.deletePolicy = (Button) convertView.findViewById(R.id.btnDeletePolicy);
            holder.layoutPolicyDetail = (LinearLayout)convertView.findViewById(R.id.LinearLayoutPolicyDetail);
            holder.layoutDown        = (LinearLayout)convertView.findViewById(R.id.LayoutLoopItemDown);
            holder.show = (ImageView)convertView.findViewById(R.id.ivLoopPolicyShowMore);
 
            //包含、排除按钮
            holder.excludeFlag.setOnClickListener(new Button.OnClickListener()
            {
                public void onClick(View v)
                {
                    TLoopPolicy policy = (TLoopPolicy) mData.get(position).get("policy");
                    int tmp = (policy.getExcludeFlag() == 0 ? 1 : 0);
                    policy.setExcludeFlag(tmp);
                    
                    if (policy.getExcludeFlag() == 1)
                    {
                        ((Button)v).setBackgroundResource(R.drawable.exclude);
                    }
                    else
                    {
                        ((Button)v).setBackgroundResource(R.drawable.include);
                    }
             
                }
            });

            convertView.setTag(holder);
        }
        else
        {
            holder = (ViewHolder) convertView.getTag();
        }
        
        holder.policy = (TLoopPolicy) mData.get(position).get("policy");

        //监听器
        //点击
        LoopPolicyButtonListener listener = (LoopPolicyButtonListener)mData.get(position).get("listener");
        listener.setTag(holder.policy);
        holder.layoutPolicyDetail.setOnClickListener(listener);
        //长按
        LoopPolicyLongClickListener LongListener = (LoopPolicyLongClickListener)mData.get(position).get("longListener");
        LongListener.setTag(holder.policy);
        holder.layoutPolicyDetail.setOnLongClickListener(LongListener);
        //删除
        LoopPolicyDeleteListener deleteListener = (LoopPolicyDeleteListener)mData.get(position).get("deleteListener");
        deleteListener.setTag(holder.policy);
        holder.deletePolicy.setOnClickListener(deleteListener);

        //排除、包含标志
        if (holder.policy.getExcludeFlag() == 1)
        {
            holder.excludeFlag.setBackgroundResource(R.drawable.exclude);
        }
        else
        {
            holder.excludeFlag.setBackgroundResource(R.drawable.include);
        }

        
        //弹出设置界面
        holder.policyInfo.setText(holder.policy.getInfo());
        holder.policyInfoMore.setText(holder.policy.getInfoMore());
    
        // 子布局
        setChildViewVisibility(holder, position);

        return convertView;
    }
    
    private void setChildViewVisibility(ViewHolder holder, final int position)
    {
        // 子布局
        holder.visibility = (Integer) mData.get(position).get("visibility");
        holder.parent = (ChildViewParent) mData.get(position).get("parent");
        ChildView newView = (ChildView) mData.get(position).get("childView");
        //策略发生变更，子视图跟着发生变更
        if(holder.childView != newView)
        {
            holder.layoutDown.setVisibility(View.GONE);
            holder.layoutDown.removeAllViews();
        }
        
        holder.childView = newView;
        if(holder.childView == null)
        {
            return;
        }
        
        holder.childView.setTag(holder.policy);
        holder.show.setVisibility(View.VISIBLE);

        if(holder.visibility != holder.layoutDown.getVisibility())
        {
            if(holder.visibility == View.VISIBLE)
            {
                if(!holder.childView.isInitialize())
                {
                    // 子布局初始化
                    holder.childView.initLayout(holder.parent);
                    holder.childView.setParentScrollView((ScrollView)mData.get(position).get("scrollview"));
                    holder.layoutDown.addView(holder.childView.getLayoutView());
                    //必须要在 addView 之后才能 new ExpandAnimation
                    //每增加或者删除一个，就是调用new ViewHolder()
                    holder.expandAni = new ExpandAnimation(holder.layoutDown, ExpandAnimation.DEFAULT_DURATION);

                }
                else
                {
                    holder.childView.updateLayout();
                }
                holder.show.setImageResource(R.drawable.arrow_up);
            }
            else
            {
                holder.childView.onClose();
                holder.show.setImageResource(R.drawable.arrow_down);
            }
            
            holder.expandAni.reset();
            holder.expandAni.setVisibility(holder.visibility);
            holder.layoutDown.startAnimation(holder.expandAni);

        }
    }

}
